package net.zzy.pbbot.plugin.daliyNews.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.daliyNews.entity.NewsResp;
import net.zzy.pbbot.util.HttpsUtils;
import net.zzy.pbbot.util.UrlUtil;
import org.springframework.stereotype.Component;

import static net.zzy.pbbot.finalfield.Api.DALIY_NEWS_URL;

/**
 * @author zhuZhaoYang
 * @date 2021/9/24 11:50
 */
@Slf4j
@Component
public class DaliyNewsService {

    public static Msg getNewsImage() {
        log.info("开始获取每日新闻");
        String resp = null;
        try {
            resp = HttpsUtils.get(DALIY_NEWS_URL, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        NewsResp newsResp = JSON.parseObject(resp, NewsResp.class);
        assert newsResp != null;
        String imageUrl = UrlUtil.getNewsImageUrl(newsResp.getUrl());
        Msg image = Msg.builder()
                .image(imageUrl);
        log.info("Msg构建完成,图片地址为{}", imageUrl);
        return image;
    }
}
